﻿Imports DTO
Imports DAO

Public Class ucDanhSachDangKyKhamBenh
    Dim DS_KhamBenh As DataTable
    Private Sub ucDanhSachDangKyKhamBenh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim query As String
        Dim DTP As New DataProvider()
        query = "select * from DANHSACHKHAMBENH"
        DS_KhamBenh = DTP.Datatable_PHONGMACHTU(query)
        dtgdanhsachkhambenh.DataSource = DS_KhamBenh
    End Sub

    Private Sub btnXuatExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuatExcel.Click

        Try
            Dim app As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
            'tao workbook
            Dim workBook As Microsoft.Office.Interop.Excel._Workbook = app.Workbooks.Add(Type.Missing)
            'tao excelsheet
            Dim workSheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
            '
            app.Visible = True
            '
            workSheet = workBook.Sheets("Sheet1")
            workSheet = workBook.ActiveSheet
            '
            workSheet.Name = "Exported"

            Dim i, j As Integer
            '
            For j = 0 To dtgdanhsachkhambenh.Columns.Count - 1
                Dim r As Microsoft.Office.Interop.Excel.Range = workSheet.Cells(1, j + 1)
                r.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Red)
                r.Font.Bold = True
                workSheet.Cells(1, j + 1) = dtgdanhsachkhambenh.Columns(j).HeaderText
            Next
            '
            For i = 0 To dtgdanhsachkhambenh.Rows.Count - 2
                For j = 0 To dtgdanhsachkhambenh.Columns.Count - 1
                    workSheet.Cells(i + 2, j + 1) = dtgdanhsachkhambenh.Rows(i).Cells(j).Value.ToString()
                Next
            Next
            workBook.SaveAs(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
